



<!doctype html>
<html lang="en" class="no-js">
  <head>

<style>
  h1, h2, h3 { color: #04A9F4; }
  pre { color: black !important; }
</style>


      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      <meta http-equiv="x-ua-compatible" content="ie=edge">

        <meta name="description" content="Open source package for Survival Analysis modeling">


        <link rel="canonical" href="https://www.pysurvival.io/tutorials/maintenance.html">


        <meta name="author" content="The PySurvival Team">


        <meta name="lang:clipboard.copy" content="Copy to clipboard">

        <meta name="lang:clipboard.copied" content="Copied to clipboard">

        <meta name="lang:search.language" content="en">

        <meta name="lang:search.pipeline.stopwords" content="True">

        <meta name="lang:search.pipeline.trimmer" content="True">

        <meta name="lang:search.result.none" content="No matching documents">

        <meta name="lang:search.result.one" content="1 matching document">

        <meta name="lang:search.result.other" content="# matching documents">

        <meta name="lang:search.tokenizer" content="[\s\-]+">

      <link rel="shortcut icon" href="../images/favicon.ico">
      <meta name="generator" content="mkdocs-1.0.4, mkdocs-material-4.0.2">



        <title>Predictive Maintenance - PySurvival</title>



      <link rel="stylesheet" href="../assets/stylesheets/application.982221ab.css">

        <link rel="stylesheet" href="../assets/stylesheets/application-palette.224b79ff.css">




        <meta name="theme-color" content="#2196f3">



      <script src="../assets/javascripts/modernizr.1f0bcf2b.js"></script>



        <link href="https://fonts.gstatic.com" rel="preconnect" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=:300,400,400i,700|">
        <style>body,input{font-family:"","Helvetica Neue",Helvetica,Arial,sans-serif}code,kbd,pre{font-family:"","Courier New",Courier,monospace}</style>


    <link rel="stylesheet" href="../assets/fonts/material-icons.css">






  </head>



    <body dir="ltr" data-md-color-primary="blue" data-md-color-accent="purple">

    <svg class="md-svg">
      <defs>


          <svg xmlns="http://www.w3.org/2000/svg" width="416" height="448"
    viewBox="0 0 416 448" id="__github">
  <path fill="currentColor" d="M160 304q0 10-3.125 20.5t-10.75 19-18.125
        8.5-18.125-8.5-10.75-19-3.125-20.5 3.125-20.5 10.75-19 18.125-8.5
        18.125 8.5 10.75 19 3.125 20.5zM320 304q0 10-3.125 20.5t-10.75
        19-18.125 8.5-18.125-8.5-10.75-19-3.125-20.5 3.125-20.5 10.75-19
        18.125-8.5 18.125 8.5 10.75 19 3.125 20.5zM360
        304q0-30-17.25-51t-46.75-21q-10.25 0-48.75 5.25-17.75 2.75-39.25
        2.75t-39.25-2.75q-38-5.25-48.75-5.25-29.5 0-46.75 21t-17.25 51q0 22 8
        38.375t20.25 25.75 30.5 15 35 7.375 37.25 1.75h42q20.5 0
        37.25-1.75t35-7.375 30.5-15 20.25-25.75 8-38.375zM416 260q0 51.75-15.25
        82.75-9.5 19.25-26.375 33.25t-35.25 21.5-42.5 11.875-42.875 5.5-41.75
        1.125q-19.5 0-35.5-0.75t-36.875-3.125-38.125-7.5-34.25-12.875-30.25-20.25-21.5-28.75q-15.5-30.75-15.5-82.75
        0-59.25 34-99-6.75-20.5-6.75-42.5 0-29 12.75-54.5 27 0 47.5 9.875t47.25
        30.875q36.75-8.75 77.25-8.75 37 0 70 8 26.25-20.5
        46.75-30.25t47.25-9.75q12.75 25.5 12.75 54.5 0 21.75-6.75 42 34 40 34
        99.5z" />
</svg>

      </defs>
    </svg>
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" data-md-component="overlay" for="__drawer"></label>

      <a href="#predicting-when-a-machine-will-break" tabindex="1" class="md-skip">
        Skip to content
      </a>


      <header class="md-header" data-md-component="header">
  <nav class="md-header-nav md-grid">
    <div class="md-flex">
      <div class="md-flex__cell md-flex__cell--shrink">
        <a href="https://www.pysurvival.io/" title="PySurvival" class="md-header-nav__button md-logo">

            <img src="../images/logo.ico" width="24" height="24">

        </a>
      </div>
      <div class="md-flex__cell md-flex__cell--shrink">
        <label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
      </div>
      <div class="md-flex__cell md-flex__cell--stretch">
        <div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">

            <span class="md-header-nav__topic">
              PySurvival
            </span>
            <span class="md-header-nav__topic">
              Predictive Maintenance
            </span>

        </div>
      </div>

              <!-- Global site tag (gtag.js) - Google Analytics -->
              <script async src="https://www.googletagmanager.com/gtag/js?id=UA-136790579-1"></script>
              <script>
                window.dataLayer = window.dataLayer || [];
                function gtag(){dataLayer.push(arguments);}
                gtag('js', new Date());

                gtag('config', 'UA-136790579-1');
              </script>

      <div class="md-flex__cell md-flex__cell--shrink">

          <label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>

<div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
      <label class="md-icon md-search__icon" for="__search"></label>
      <button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">
        &#xE5CD;
      </button>
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="result">
          <div class="md-search-result__meta">
            Type to start searching
          </div>
          <ol class="md-search-result__list"></ol>
        </div>
      </div>
    </div>
  </div>
</div>

      </div>

        <div class="md-flex__cell md-flex__cell--shrink">
          <div class="md-header-nav__source">





<a href="https://github.com/square/pysurvival/" title="Go to repository" class="md-source" >

    <div class="md-source__icon">
      <svg viewBox="0 0 24 24" width="24" height="24">
        <use xlink:href="#__github" width="24" height="24"></use>
      </svg>
    </div>

  <div class="md-source__repository">
    square/pysurvival
  </div>
</a>
          </div>
        </div>

    </div>
  </nav>
</header>

    <div class="md-container">




      <main class="md-main">
        <div class="md-main__inner md-grid" data-md-component="container">


              <div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    <nav class="md-nav md-nav--primary" data-md-level="0">
  <label class="md-nav__title md-nav__title--site" for="__drawer">
    <a href="https://www.pysurvival.io/" title="PySurvival" class="md-nav__button md-logo">

        <img src="../images/logo.ico" width="48" height="48">

    </a>
    PySurvival
  </label>

    <div class="md-nav__source">





<a href="https://github.com/square/pysurvival/" title="Go to repository" class="md-source" >

    <div class="md-source__icon">
      <svg viewBox="0 0 24 24" width="24" height="24">
        <use xlink:href="#__github" width="24" height="24"></use>
      </svg>
    </div>

  <div class="md-source__repository">
    square/pysurvival
  </div>
</a>
    </div>

  <ul class="md-nav__list" data-md-scrollfix>






  <li class="md-nav__item">
    <a href="../index.html" title="Home" class="md-nav__link">
      Home
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../installation.html" title="Installation" class="md-nav__link">
      Installation
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../intro.html" title="Introduction to Survival Analysis" class="md-nav__link">
      Introduction to Survival Analysis
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../math.html" title="The math of Survival Analysis" class="md-nav__link">
      The math of Survival Analysis
    </a>
  </li>









  <li class="md-nav__item md-nav__item--active md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-5" type="checkbox" id="nav-5" checked>

    <label class="md-nav__link" for="nav-5">
      Tutorials
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-5">
        Tutorials
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="churn.html" title="Churn Prediction" class="md-nav__link">
      Churn Prediction
    </a>
  </li>







  <li class="md-nav__item">
    <a href="credit_risk.html" title="Credit Risk" class="md-nav__link">
      Credit Risk
    </a>
  </li>







  <li class="md-nav__item">
    <a href="employee_retention.html" title="Employee Retention" class="md-nav__link">
      Employee Retention
    </a>
  </li>









  <li class="md-nav__item md-nav__item--active">

    <input class="md-toggle md-nav__toggle" data-md-toggle="toc" type="checkbox" id="__toc">




      <label class="md-nav__link md-nav__link--active" for="__toc">
        Predictive Maintenance
      </label>

    <a href="maintenance.html" title="Predictive Maintenance" class="md-nav__link md-nav__link--active">
      Predictive Maintenance
    </a>


<nav class="md-nav md-nav--secondary">





    <label class="md-nav__title" for="__toc">Table of contents</label>
    <ul class="md-nav__list" data-md-scrollfix>

        <li class="md-nav__item">
  <a href="#1-introduction" title="1 - Introduction" class="md-nav__link">
    1 - Introduction
  </a>

</li>

        <li class="md-nav__item">
  <a href="#2-set-up" title="2 - Set up" class="md-nav__link">
    2 - Set up
  </a>

</li>

        <li class="md-nav__item">
  <a href="#3-dataset" title="3 - Dataset" class="md-nav__link">
    3 - Dataset
  </a>

</li>

        <li class="md-nav__item">
  <a href="#4-exploratory-data-analysis" title="4 - Exploratory Data Analysis" class="md-nav__link">
    4 - Exploratory Data Analysis
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#41-null-values-and-duplicates" title="4.1 - Null values and duplicates" class="md-nav__link">
    4.1 - Null values and duplicates
  </a>

</li>

          <li class="md-nav__item">
  <a href="#42-visual-exploration-and-statistics" title="4.2 - Visual exploration and statistics" class="md-nav__link">
    4.2 - Visual exploration and statistics
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#421-numerical-features" title="4.2.1 - Numerical features" class="md-nav__link">
    4.2.1 - Numerical features
  </a>

</li>

          <li class="md-nav__item">
  <a href="#422-categorical-features" title="4.2.2 - Categorical features" class="md-nav__link">
    4.2.2 - Categorical features
  </a>

</li>

          <li class="md-nav__item">
  <a href="#423-time-event" title="4.2.3 - Time &amp; Event" class="md-nav__link">
    4.2.3 - Time &amp; Event
  </a>

</li>

      </ul>
    </nav>

</li>

          <li class="md-nav__item">
  <a href="#43-correlations" title="4.3 - Correlations" class="md-nav__link">
    4.3 - Correlations
  </a>

</li>

      </ul>
    </nav>

</li>

        <li class="md-nav__item">
  <a href="#5-modeling" title="5 - Modeling" class="md-nav__link">
    5 - Modeling
  </a>

</li>

        <li class="md-nav__item">
  <a href="#6-cross-validation" title="6 - Cross Validation" class="md-nav__link">
    6 - Cross Validation
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#61-c-index" title="6.1 - C-index" class="md-nav__link">
    6.1 - C-index
  </a>

</li>

          <li class="md-nav__item">
  <a href="#62-brier-score" title="6.2 - Brier Score" class="md-nav__link">
    6.2 - Brier Score
  </a>

</li>

      </ul>
    </nav>

</li>

        <li class="md-nav__item">
  <a href="#7-predictions" title="7 - Predictions" class="md-nav__link">
    7 - Predictions
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#71-overall-predictions" title="7.1 - Overall predictions" class="md-nav__link">
    7.1 - Overall predictions
  </a>

</li>

          <li class="md-nav__item">
  <a href="#72-individual-predictions" title="7.2 - Individual predictions" class="md-nav__link">
    7.2 - Individual predictions
  </a>

</li>

      </ul>
    </nav>

</li>

        <li class="md-nav__item">
  <a href="#8-conclusion" title="8 - Conclusion" class="md-nav__link">
    8 - Conclusion
  </a>

</li>

        <li class="md-nav__item">
  <a href="#references" title="References" class="md-nav__link">
    References
  </a>

</li>





    </ul>

</nav>

  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6" type="checkbox" id="nav-6">

    <label class="md-nav__link" for="nav-6">
      Models
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-6">
        Models
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-1" type="checkbox" id="nav-6-1">

    <label class="md-nav__link" for="nav-6-1">
      Cox Proportional Hazard
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-1">
        Cox Proportional Hazard
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/coxph.html" title="Standard CoxPH  (API)" class="md-nav__link">
      Standard CoxPH  (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/nonlinear_coxph.html" title="DeepSurv/Nonlinear CoxPH (API)" class="md-nav__link">
      DeepSurv/Nonlinear CoxPH (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/semi_parametric.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-2" type="checkbox" id="nav-6-2">

    <label class="md-nav__link" for="nav-6-2">
      Multi-Task Logistic Regression (MTLR)
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-2">
        Multi-Task Logistic Regression (MTLR)
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/linear_mtlr.html" title="Linear MTLR (API)" class="md-nav__link">
      Linear MTLR (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/neural_mtlr.html" title="Neural MTLR (API)" class="md-nav__link">
      Neural MTLR (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/mtlr_theory.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-3" type="checkbox" id="nav-6-3">

    <label class="md-nav__link" for="nav-6-3">
      Non-Parametric
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-3">
        Non-Parametric
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/kaplan_meier.html" title="Kaplan Meier (API)" class="md-nav__link">
      Kaplan Meier (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/smooth_kaplan_meier.html" title="Smoothed Kaplan Meier (API)" class="md-nav__link">
      Smoothed Kaplan Meier (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/non_parametric.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-4" type="checkbox" id="nav-6-4">

    <label class="md-nav__link" for="nav-6-4">
      Parametric
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-4">
        Parametric
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/parametric.html" title="Parametric models (API)" class="md-nav__link">
      Parametric models (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/parametric_theory.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-5" type="checkbox" id="nav-6-5">

    <label class="md-nav__link" for="nav-6-5">
      Simulation
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-5">
        Simulation
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/simulations.html" title="Simulation models (API)" class="md-nav__link">
      Simulation models (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/simulations_theory.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-6" type="checkbox" id="nav-6-6">

    <label class="md-nav__link" for="nav-6-6">
      Survival Forest
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-6">
        Survival Forest
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/conditional_survival_forest.html" title="Conditional Survival Forest (API)" class="md-nav__link">
      Conditional Survival Forest (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/extra_survival_trees.html" title="Extra Survival Trees (API)" class="md-nav__link">
      Extra Survival Trees (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/random_survival_forest.html" title="Random Survival Forest (API)" class="md-nav__link">
      Random Survival Forest (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/survival_forest.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-7" type="checkbox" id="nav-6-7">

    <label class="md-nav__link" for="nav-6-7">
      Survival SVM
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-7">
        Survival SVM
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/linear_svm.html" title="Linear Survival SVM (API)" class="md-nav__link">
      Linear Survival SVM (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/kernel_svm.html" title="Kernel Survival SVM (API)" class="md-nav__link">
      Kernel Survival SVM (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/survival_svm.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-7" type="checkbox" id="nav-7">

    <label class="md-nav__link" for="nav-7">
      Performance metrics
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-7">
        Performance metrics
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../metrics/c_index.html" title="C-index" class="md-nav__link">
      C-index
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../metrics/brier_score.html" title="Brier Score" class="md-nav__link">
      Brier Score
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-8" type="checkbox" id="nav-8">

    <label class="md-nav__link" for="nav-8">
      Miscellaneous
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-8">
        Miscellaneous
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../miscellaneous/activation_functions.html" title="Activation Functions" class="md-nav__link">
      Activation Functions
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../miscellaneous/save_load.html" title="Saving/Loading models" class="md-nav__link">
      Saving/Loading models
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../miscellaneous/tips.html" title="Tips for fitting models" class="md-nav__link">
      Tips for fitting models
    </a>
  </li>


      </ul>
    </nav>
  </li>


  </ul>
</nav>
                  </div>
                </div>
              </div>


              <div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">

<nav class="md-nav md-nav--secondary">





    <label class="md-nav__title" for="__toc">Table of contents</label>
    <ul class="md-nav__list" data-md-scrollfix>

        <li class="md-nav__item">
  <a href="#1-introduction" title="1 - Introduction" class="md-nav__link">
    1 - Introduction
  </a>

</li>

        <li class="md-nav__item">
  <a href="#2-set-up" title="2 - Set up" class="md-nav__link">
    2 - Set up
  </a>

</li>

        <li class="md-nav__item">
  <a href="#3-dataset" title="3 - Dataset" class="md-nav__link">
    3 - Dataset
  </a>

</li>

        <li class="md-nav__item">
  <a href="#4-exploratory-data-analysis" title="4 - Exploratory Data Analysis" class="md-nav__link">
    4 - Exploratory Data Analysis
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#41-null-values-and-duplicates" title="4.1 - Null values and duplicates" class="md-nav__link">
    4.1 - Null values and duplicates
  </a>

</li>

          <li class="md-nav__item">
  <a href="#42-visual-exploration-and-statistics" title="4.2 - Visual exploration and statistics" class="md-nav__link">
    4.2 - Visual exploration and statistics
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#421-numerical-features" title="4.2.1 - Numerical features" class="md-nav__link">
    4.2.1 - Numerical features
  </a>

</li>

          <li class="md-nav__item">
  <a href="#422-categorical-features" title="4.2.2 - Categorical features" class="md-nav__link">
    4.2.2 - Categorical features
  </a>

</li>

          <li class="md-nav__item">
  <a href="#423-time-event" title="4.2.3 - Time &amp; Event" class="md-nav__link">
    4.2.3 - Time &amp; Event
  </a>

</li>

      </ul>
    </nav>

</li>

          <li class="md-nav__item">
  <a href="#43-correlations" title="4.3 - Correlations" class="md-nav__link">
    4.3 - Correlations
  </a>

</li>

      </ul>
    </nav>

</li>

        <li class="md-nav__item">
  <a href="#5-modeling" title="5 - Modeling" class="md-nav__link">
    5 - Modeling
  </a>

</li>

        <li class="md-nav__item">
  <a href="#6-cross-validation" title="6 - Cross Validation" class="md-nav__link">
    6 - Cross Validation
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#61-c-index" title="6.1 - C-index" class="md-nav__link">
    6.1 - C-index
  </a>

</li>

          <li class="md-nav__item">
  <a href="#62-brier-score" title="6.2 - Brier Score" class="md-nav__link">
    6.2 - Brier Score
  </a>

</li>

      </ul>
    </nav>

</li>

        <li class="md-nav__item">
  <a href="#7-predictions" title="7 - Predictions" class="md-nav__link">
    7 - Predictions
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#71-overall-predictions" title="7.1 - Overall predictions" class="md-nav__link">
    7.1 - Overall predictions
  </a>

</li>

          <li class="md-nav__item">
  <a href="#72-individual-predictions" title="7.2 - Individual predictions" class="md-nav__link">
    7.2 - Individual predictions
  </a>

</li>

      </ul>
    </nav>

</li>

        <li class="md-nav__item">
  <a href="#8-conclusion" title="8 - Conclusion" class="md-nav__link">
    8 - Conclusion
  </a>

</li>

        <li class="md-nav__item">
  <a href="#references" title="References" class="md-nav__link">
    References
  </a>

</li>





    </ul>

</nav>
                  </div>
                </div>
              </div>


          <div class="md-content">
            <article class="md-content__inner md-typeset">


                  <a href="https://github.com/square/pysurvival/edit/master/docs/tutorials/maintenance.md" title="Edit this page" class="md-icon md-content__icon">&#xE3C9;</a>


                <!--  Tutorial - Predictive Maintenance -->

<style>
  h1, h2, h3, h4 { color: #04A9F4; }
</style>

<h1 id="predicting-when-a-machine-will-break">Predicting when a machine will break</h1>
<h2 id="1-introduction">1 - Introduction</h2>
<p><a href="https://en.wikipedia.org/wiki/Predictive_maintenance">Predictive Maintenance (PdM)</a> is a great application of Survival Analysis since it consists in predicting when equipment failure will occur and therefore alerting the maintenance team to prevent that failure. Indeed, accurately modeling if and when a machine will break is crucial for industrial and manufacturing businesses as it can help:</p>
<ul>
<li>maintain a safe work environment by ensuring that machines are working properly</li>
<li>increase productivity by preventing unplanned reactive maintenance and minimizing downtime</li>
<li>optimize costs by removing the need for too many unnecessary checks or repairs of components -- a.k.a preventative maintenance</li>
</ul>
<p>Within the past couple of years, thanks to the use of <a href="https://en.wikipedia.org/wiki/Internet_of_things#Manufacturing">Internet of Things (IoT) technologies</a>, a plethora of data has been generated by various sensors on machines, mechanical and electrical components, such as temperature, vibration, voltage or pressure. This type of information can be used to predict future failures.</p>
<hr />
<h2 id="2-set-up">2 - Set up</h2>
<p>We will consider that a manufacturing company uses many machines to build their final products. The factory is using IoT technologies via smart sensors to measure and save various kind of inputs from the physical environment and the state of their equipment.</p>
<p>Unfortunately, every time a machine breaks the production is stopped, which costs the company thousands of dollars in repair and late delivery fees. The factory manager asks the company's Data Science team if it is possible to find a way to be more pro-active so as to optimize spending.</p>
<hr />
<h2 id="3-dataset">3 - Dataset</h2>
<p>The dataset the team wants to use contains the following features:</p>
<table>
<thead>
<tr>
<th>Feature category</th>
<th>Feature name</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><span style="color:blue"> Time </span></td>
<td><code>lifetime</code></td>
<td>numerical</td>
<td>Number of weeks the machine has been active</td>
</tr>
<tr>
<td><span style="color:blue"> Event </span></td>
<td><code>broken</code></td>
<td>numerical</td>
<td>Specifies if the machine was broken or hasn't been broken yet for the corresponding weeks in activity</td>
</tr>
<tr>
<td>IoT measure</td>
<td><code>pressureInd</code></td>
<td>numerical</td>
<td>The pressure index is used to quantify the flow of liquid through pipes, as a sudden drop of pressure can indicate a leak</td>
</tr>
<tr>
<td>IoT measure</td>
<td><code>moistureInd</code></td>
<td>numerical</td>
<td>The moisture index is a measure of the relative humidity in the air. It is important to keep track of it as excessive humidity can create mold and damage the equipment</td>
</tr>
<tr>
<td>IoT measure</td>
<td><code>temperatureInd</code></td>
<td>numerical</td>
<td>The temperature index of the machine is computed using voltage devices called thermocouples that translate a change in voltage into temperature measure. It is recorded to avoid damages to electric circuits, fire or even explosion</td>
</tr>
<tr>
<td>Company feature</td>
<td><code>team</code></td>
<td>categorical</td>
<td>This indicator specifies which team is using the machine</td>
</tr>
<tr>
<td>Machine feature</td>
<td><code>provider</code></td>
<td>categorical</td>
<td>This indicator specifies the name of the machine manufacturer</td>
</tr>
</tbody>
</table>
<hr />
<h2 id="4-exploratory-data-analysis">4 - Exploratory Data Analysis</h2>
<p>Let's perform an exploratory data analysis (EDA) so as to understand what the data look like and start answering interesting questions about our problem.</p>
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Importing modules</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">matplotlib</span> <span style="color: #008000; font-weight: bold">import</span> pyplot <span style="color: #008000; font-weight: bold">as</span> plt
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.datasets</span> <span style="color: #008000; font-weight: bold">import</span> Dataset
<span style="color: #666666">%</span>pylab inline

<span style="color: #408080; font-style: italic"># Reading the dataset</span>
raw_dataset <span style="color: #666666">=</span> Dataset(<span style="color: #BA2121">&#39;maintenance&#39;</span>)<span style="color: #666666">.</span>load()
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;The raw_dataset has the following shape: {}.&quot;</span><span style="color: #666666">.</span>format(raw_dataset<span style="color: #666666">.</span>shape))
raw_dataset<span style="color: #666666">.</span>head(<span style="color: #666666">3</span>)
</pre></div>

<p>Here is an overview of the raw dataset:</p>
<table>
<thead>
<tr>
<th>lifetime</th>
<th>broken</th>
<th>pressureInd</th>
<th>moistureInd</th>
<th>temperatureInd</th>
<th>team</th>
<th>provider</th>
</tr>
</thead>
<tbody>
<tr>
<td>56</td>
<td>0</td>
<td>92.17</td>
<td>104.23</td>
<td>96.51</td>
<td>TeamA</td>
<td>Provider4</td>
</tr>
<tr>
<td>81</td>
<td>1</td>
<td>72.07</td>
<td>103.06</td>
<td>87.27</td>
<td>TeamC</td>
<td>Provider4</td>
</tr>
<tr>
<td>60</td>
<td>0</td>
<td>96.27</td>
<td>77.80</td>
<td>112.19</td>
<td>TeamA</td>
<td>Provider1</td>
</tr>
</tbody>
</table>
<p>The following command is also very useful so as to assess the type of variables we're dealing with:
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>raw_dataset<span style="color: #666666">.</span>info()
</pre></div></p>
<p>There are 3 numerical features (<code>pressureInd</code>, <code>moistureInd</code>, <code>temperatureInd</code>) and 2 categorical features (<code>team</code>, <code>provider</code>).
Let's encode the categorical variables as  one-hot vectors and define the modeling features:
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Defining the time and event column</span>
time_column <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;lifetime&#39;</span>
event_column <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;broken&#39;</span>

<span style="color: #408080; font-style: italic"># Encoding the categorical variables as one-hot vectors</span>
categories <span style="color: #666666">=</span> [<span style="color: #BA2121">&#39;provider&#39;</span>, <span style="color: #BA2121">&#39;team&#39;</span>]
dataset <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>get_dummies(raw_dataset, columns <span style="color: #666666">=</span> categories, drop_first<span style="color: #666666">=</span><span style="color: #008000">True</span>)

<span style="color: #408080; font-style: italic"># Defining the modeling features</span>
features <span style="color: #666666">=</span> np<span style="color: #666666">.</span>setdiff1d(dataset<span style="color: #666666">.</span>columns, [<span style="color: #BA2121">&#39;lifetime&#39;</span>, <span style="color: #BA2121">&#39;broken&#39;</span>])<span style="color: #666666">.</span>tolist()
</pre></div></p>
<h3 id="41-null-values-and-duplicates">4.1 - Null values and duplicates</h3>
<p>The first thing to do is checking if the dataset contains Null values and if it has duplicated rows.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Checking for null values</span>
N_null <span style="color: #666666">=</span> <span style="color: #008000">sum</span>(dataset[features]<span style="color: #666666">.</span>isnull()<span style="color: #666666">.</span>sum())
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;The dataset contains {} null values&quot;</span><span style="color: #666666">.</span>format(N_null)) <span style="color: #408080; font-style: italic">#0 null values</span>

<span style="color: #408080; font-style: italic"># Removing duplicates if there exist</span>
N_dupli <span style="color: #666666">=</span> <span style="color: #008000">sum</span>(dataset<span style="color: #666666">.</span>duplicated(keep<span style="color: #666666">=</span><span style="color: #BA2121">&#39;first&#39;</span>))
dataset <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>drop_duplicates(keep<span style="color: #666666">=</span><span style="color: #BA2121">&#39;first&#39;</span>)<span style="color: #666666">.</span>reset_index(drop<span style="color: #666666">=</span><span style="color: #008000">True</span>)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;The dataset contains {} duplicates&quot;</span><span style="color: #666666">.</span>format(N_dupli))

<span style="color: #408080; font-style: italic"># Number of samples in the dataset</span>
N <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>shape[<span style="color: #666666">0</span>]
</pre></div>
As it turns out the dataset doesn't have any Null values or duplicates.</p>
<h3 id="42-visual-exploration-and-statistics">4.2 - Visual exploration and statistics</h3>
<p>Let's check out/visualize the feature statistics:</p>
<h4 id="421-numerical-features">4.2.1 - Numerical features</h4>
<p>We will display the boxplot and histogram of each feature
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">for</span> feature <span style="color: #AA22FF; font-weight: bold">in</span> [<span style="color: #BA2121">&#39;pressureInd&#39;</span>,<span style="color: #BA2121">&#39;moistureInd&#39;</span>,<span style="color: #BA2121">&#39;temperatureInd&#39;</span>]:

    <span style="color: #408080; font-style: italic"># Creating an empty chart</span>
    fig, ((ax1, ax2)) <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(<span style="color: #666666">1</span>, <span style="color: #666666">2</span>,  figsize<span style="color: #666666">=</span>(<span style="color: #666666">15</span>, <span style="color: #666666">4</span>))

    <span style="color: #408080; font-style: italic"># Extracting the feature values</span>
    x <span style="color: #666666">=</span> raw_dataset[feature]<span style="color: #666666">.</span>values

    <span style="color: #408080; font-style: italic"># Boxplot</span>
    ax1<span style="color: #666666">.</span>boxplot(x)
    ax1<span style="color: #666666">.</span>set_title( <span style="color: #BA2121">&#39;Boxplot for {}&#39;</span><span style="color: #666666">.</span>format(feature) )

    <span style="color: #408080; font-style: italic"># Histogram</span>
    ax2<span style="color: #666666">.</span>hist(x, bins<span style="color: #666666">=20</span>)
    ax2<span style="color: #666666">.</span>set_title( <span style="color: #BA2121">&#39;Histogram for {}&#39;</span><span style="color: #666666">.</span>format(feature) )

    <span style="color: #408080; font-style: italic"># Display</span>
    plt<span style="color: #666666">.</span>show()
</pre></div></p>
<p><center><img src="images/maintenance_moisture.png" alt="PySurvival - Predictive Maintenance Tutorial - Moisture Boxplot and Histogram" title="PySurvival - Predictive Maintenance Tutorial - Moisture Boxplot and Histogram" width=100%, height=100%  /></center>
<center>Figure 1 - Boxplot and histogram for moistureInd</center></p>
<p><center><img src="images/maintenance_pressure.png" alt="PySurvival - Predictive Maintenance Tutorial - Pressure Boxplot and Histogram" title="PySurvival - Predictive Maintenance Tutorial - Pressure Boxplot and Histogram" width=100%, height=100%  /></center>
<center>Figure 2 - Boxplot and histogram for pressureInd</center></p>
<p><center><img src="images/maintenance_temperature.png" alt="PySurvival - Predictive Maintenance Tutorial - Temperature Boxplot and Histogram" title="PySurvival - Predictive Maintenance Tutorial - Temperature Boxplot and Histogram" width=100%, height=100%  /></center>
<center>Figure 3 - Boxplot and histogram for temperatureInd</center></p>
<p>These features have very few outliers (<em>here, there's no real need to remove them, but you can if you prefer</em>) and seem to follow normal distributions.</p>
<h4 id="422-categorical-features">4.2.2 - Categorical features</h4>
<p>We will display the occurrences of the categories in a barchart for each feature
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">collections</span> <span style="color: #008000; font-weight: bold">import</span> Counter
<span style="color: #008000; font-weight: bold">for</span> feature <span style="color: #AA22FF; font-weight: bold">in</span> [<span style="color: #BA2121">&#39;team&#39;</span>,<span style="color: #BA2121">&#39;provider&#39;</span>]:

    <span style="color: #408080; font-style: italic"># Creating an empty chart</span>
    fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(figsize<span style="color: #666666">=</span>(<span style="color: #666666">15</span>, <span style="color: #666666">4</span>))

    <span style="color: #408080; font-style: italic"># Extracting the feature values</span>
    x <span style="color: #666666">=</span> raw_dataset[feature]<span style="color: #666666">.</span>values

    <span style="color: #408080; font-style: italic"># Counting the number of occurrences for each category</span>
    data <span style="color: #666666">=</span> Counter(x)
    category <span style="color: #666666">=</span> <span style="color: #008000">list</span>(data<span style="color: #666666">.</span>keys())
    counts <span style="color: #666666">=</span> <span style="color: #008000">list</span>(data<span style="color: #666666">.</span>values())

    <span style="color: #408080; font-style: italic"># Boxplot</span>
    ax<span style="color: #666666">.</span>bar(category, counts)

    <span style="color: #408080; font-style: italic"># Display</span>
    plt<span style="color: #666666">.</span>title( <span style="color: #BA2121">&#39;Barchart for {}&#39;</span><span style="color: #666666">.</span>format(feature) )
    plt<span style="color: #666666">.</span>show()
</pre></div></p>
<p><center><img src="images/maintenance_team.png" alt="PySurvival - Predictive Maintenance Tutorial - Team Barchart" title="PySurvival - Predictive Maintenance Tutorial - Team Barchart" width=100%, height=100%  /></center>
<center>Figure 4 - Barchart for team</center></p>
<p><center><img src="images/maintenance_provider.png" alt="PySurvival - Predictive Maintenance Tutorial - Provider Barchart" title="PySurvival - Predictive Maintenance Tutorial - Provider Barchart" width=100%, height=100%  /></center>
<center>Figure 5 - Barchart for provider</center></p>
<p>These features seem to be uniformly distributed.</p>
<h4 id="423-time-event">4.2.3 - Time &amp; Event</h4>
<p>We will display the occurrences of event and censorship, as well as the distribution of the time output variable for both situations.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Creating an empty chart</span>
fig, ((ax1, ax2)) <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(<span style="color: #666666">1</span>, <span style="color: #666666">2</span>,  figsize<span style="color: #666666">=</span>(<span style="color: #666666">15</span>, <span style="color: #666666">4</span>))

<span style="color: #408080; font-style: italic"># Counting the number of occurrences for each category </span>
data <span style="color: #666666">=</span> Counter(raw_dataset[<span style="color: #BA2121">&#39;broken&#39;</span>]<span style="color: #666666">.</span>replace({<span style="color: #666666">0</span>:<span style="color: #BA2121">&#39;not broken yet&#39;</span>, <span style="color: #666666">1</span>:<span style="color: #BA2121">&#39;broken&#39;</span>}))
category <span style="color: #666666">=</span> <span style="color: #008000">list</span>(data<span style="color: #666666">.</span>keys())
counts <span style="color: #666666">=</span> <span style="color: #008000">list</span>(data<span style="color: #666666">.</span>values())
idx <span style="color: #666666">=</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(counts))

<span style="color: #408080; font-style: italic"># Displaying the occurrences of the event/censoring</span>
ax1<span style="color: #666666">.</span>bar(idx, counts)
ax1<span style="color: #666666">.</span>set_xticks(idx)
ax1<span style="color: #666666">.</span>set_xticklabels(category)
ax1<span style="color: #666666">.</span>set_title( <span style="color: #BA2121">&#39;Occurences of the event/censoring&#39;</span>, fontsize<span style="color: #666666">=15</span>)

<span style="color: #408080; font-style: italic"># Showing the histogram of the survival times for the censoring</span>
time_0 <span style="color: #666666">=</span> raw_dataset<span style="color: #666666">.</span>loc[ raw_dataset[<span style="color: #BA2121">&#39;broken&#39;</span>] <span style="color: #666666">==</span> <span style="color: #666666">0</span>, <span style="color: #BA2121">&#39;lifetime&#39;</span>]
ax2<span style="color: #666666">.</span>hist(time_0, bins<span style="color: #666666">=30</span>, alpha<span style="color: #666666">=0.3</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">&#39;blue&#39;</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;not broken yet&#39;</span>)

<span style="color: #408080; font-style: italic"># Showing the histogram of the survival times for the events</span>
time_1 <span style="color: #666666">=</span> raw_dataset<span style="color: #666666">.</span>loc[ raw_dataset[<span style="color: #BA2121">&#39;broken&#39;</span>] <span style="color: #666666">==</span> <span style="color: #666666">1</span>, <span style="color: #BA2121">&#39;lifetime&#39;</span>]
ax2<span style="color: #666666">.</span>hist(time_1, bins<span style="color: #666666">=20</span>, alpha<span style="color: #666666">=0.7</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">&#39;black&#39;</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;broken&#39;</span>)
ax2<span style="color: #666666">.</span>set_title( <span style="color: #BA2121">&#39;Histogram - survival time&#39;</span>, fontsize<span style="color: #666666">=15</span>)

<span style="color: #408080; font-style: italic"># Displaying everything side-by-side</span>
plt<span style="color: #666666">.</span>legend(fontsize<span style="color: #666666">=15</span>)
plt<span style="color: #666666">.</span>show()
</pre></div></p>
<p><center><img src="images/maintenance_time_event.png" alt="PySurvival - Predictive Maintenance Tutorial - Time/Event summary" title="PySurvival - Predictive Maintenance Tutorial - Time/Event summary" width=100%, height=100%  /></center>
<center>Figure 6 - Time/Event summary</center></p>
<p>Here, we can see that 2/3 of the data is censored and that the failures start happening when the machine has been active for at least 60 weeks.</p>
<h3 id="43-correlations">4.3 - Correlations</h3>
<p>Let's compute and visualize the correlation between the features
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.display</span> <span style="color: #008000; font-weight: bold">import</span> correlation_matrix
correlation_matrix(dataset[features], figure_size<span style="color: #666666">=</span>(<span style="color: #666666">15</span>, <span style="color: #666666">5</span>))
</pre></div></p>
<p><center><img src="images/maintenance_corr.png" alt="PySurvival - Predictive Maintenance Tutorial - Correlations" title="PySurvival - Predictive Maintenance Tutorial - Correlations" width=100%, height=100%  /></center>
<center>Figure 7 - Correlations</center></p>
<p>As we can see, there aren't any alarming correlations.</p>
<hr />
<h2 id="5-modeling">5 - Modeling</h2>
<p>So as to perform cross-validation later on and assess the performances of the model, let's split the dataset into training and testing sets.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Building training and testing sets</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
index_train, index_test <span style="color: #666666">=</span> train_test_split( <span style="color: #008000">range</span>(N), test_size <span style="color: #666666">=</span> <span style="color: #666666">0.4</span>)
data_train <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>loc[index_train]<span style="color: #666666">.</span>reset_index( drop <span style="color: #666666">=</span> <span style="color: #008000">True</span> )
data_test  <span style="color: #666666">=</span> dataset<span style="color: #666666">.</span>loc[index_test]<span style="color: #666666">.</span>reset_index( drop <span style="color: #666666">=</span> <span style="color: #008000">True</span> )

<span style="color: #408080; font-style: italic"># Creating the X, T and E inputs</span>
X_train, X_test <span style="color: #666666">=</span> data_train[features], data_test[features]
T_train, T_test <span style="color: #666666">=</span> data_train[time_column], data_test[time_column]
E_train, E_test <span style="color: #666666">=</span> data_train[event_column], data_test[event_column]
</pre></div></p>
<p>Let's now fit a <a href="../models/linear_mtlr.html">Linear MTLR</a> model to the training set.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.models.multi_task</span> <span style="color: #008000; font-weight: bold">import</span> LinearMultiTaskModel

<span style="color: #408080; font-style: italic"># Initializing the MTLR with a time axis split into 300 intervals</span>
linear_mtlr <span style="color: #666666">=</span> LinearMultiTaskModel(bins<span style="color: #666666">=300</span>)

<span style="color: #408080; font-style: italic"># Fitting the model</span>
linear_mtlr<span style="color: #666666">.</span>fit(X_train, T_train, E_train, num_epochs <span style="color: #666666">=</span> <span style="color: #666666">1000</span>,
                init_method <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;orthogonal&#39;</span>, optimizer <span style="color: #666666">=</span><span style="color: #BA2121">&#39;rmsprop&#39;</span>,
                lr <span style="color: #666666">=</span> <span style="color: #666666">1e-3</span>, l2_reg <span style="color: #666666">=</span> <span style="color: #666666">3</span>,  l2_smooth <span style="color: #666666">=</span> <span style="color: #666666">3</span>, )
</pre></div></p>
<p>We can take a look at the loss function values</p>
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.display</span> <span style="color: #008000; font-weight: bold">import</span> display_loss_values
display_loss_values(linear_mtlr, figure_size<span style="color: #666666">=</span>(<span style="color: #666666">7</span>, <span style="color: #666666">4</span>))
</pre></div>

<p><center><img src="images/maintenance_loss.png" alt="PySurvival - Predictive Maintenance Tutorial - Linear MTLR Loss function values" title="PySurvival - Predictive Maintenance Tutorial - Linear MTLR Loss function values" width=50%, height=50%  /></center>
<center>Figure 8 - Linear MTLR loss function values</center></p>
<hr />
<h2 id="6-cross-validation">6 - Cross Validation</h2>
<p>In order to assess the model performance, we previously split the original dataset into training and testing sets, so that we can now compute its performance metrics on the testing set:</p>
<h3 id="61-c-index">6.1 - <a href="../metrics/c_index.html">C-index</a></h3>
<p>The <a href="../metrics/c_index.html">C-index</a> represents the global assessment of the model discrimination power: <strong><em>this is the model’s ability to correctly provide a reliable ranking of the survival times based on the individual risk scores</em></strong>. In general, when the C-index is close to 1, the model has an almost perfect discriminatory power; but if it is close to 0.5, it has no ability to discriminate between low and high risk subjects.</p>
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.metrics</span> <span style="color: #008000; font-weight: bold">import</span> concordance_index
c_index <span style="color: #666666">=</span> concordance_index(linear_mtlr, X_test, T_test, E_test)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;C-index: {:.2f}&#39;</span><span style="color: #666666">.</span>format(c_index)) <span style="color: #408080; font-style: italic">#0.92</span>
</pre></div>

<p>As the c-index (0.92 here) is close to 1, it seems that the model will yield satisfactory results in terms of survival times predictions.</p>
<h3 id="62-brier-score">6.2 - <a href="../metrics/brier_score.html">Brier Score</a></h3>
<p>The <strong><em><a href="../metrics/brier_score.html">Brier score</a> measures the average discrepancies between the status and the estimated probabilities at a given time.</em></strong>
Thus, the lower the score (<em>usually below 0.25</em>), the better the predictive performance. To assess the overall error measure across multiple time points, the Integrated Brier Score (IBS) is usually computed as well.</p>
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.display</span> <span style="color: #008000; font-weight: bold">import</span> integrated_brier_score
integrated_brier_score(linear_mtlr, X_test, T_test, E_test, t_max<span style="color: #666666">=100</span>,
                       figure_size<span style="color: #666666">=</span>(<span style="color: #666666">20</span>, <span style="color: #666666">6.5</span>) )
</pre></div>

<p><center><img src="images/maintenance_brier_1.png" alt="PySurvival - Predictive Maintenance Tutorial - Linear MTLR - Brier score & Prediction error curve" title="PySurvival - Predictive Maintenance Tutorial - Linear MTLR - Brier score & Prediction error curve" width=100%, height=100%  /></center>
<center>Figure 9 - Linear MTLR - Brier scores &amp; Prediction error curve </center></p>
<p>The IBS is very close to 0.0 on the entire model time axis. This indicates that the model will have very good predictive abilities.</p>
<hr />
<h2 id="7-predictions">7 - Predictions</h2>
<h3 id="71-overall-predictions">7.1 - Overall predictions</h3>
<p>Now that we have built a model that seems to provide great performances, let's compare the time series of the actual and predicted number of machines experiencing a failure, for each time t.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.display</span> <span style="color: #008000; font-weight: bold">import</span> compare_to_actual
results <span style="color: #666666">=</span> compare_to_actual(linear_mtlr, X_test, T_test, E_test,
                            is_at_risk <span style="color: #666666">=</span> <span style="color: #008000">False</span>,  figure_size<span style="color: #666666">=</span>(<span style="color: #666666">16</span>, <span style="color: #666666">6</span>),
                            metrics <span style="color: #666666">=</span> [<span style="color: #BA2121">&#39;rmse&#39;</span>, <span style="color: #BA2121">&#39;mean&#39;</span>, <span style="color: #BA2121">&#39;median&#39;</span>])
</pre></div></p>
<p><center><img src="images/maintenance_global_pred_1.png" alt="PySurvival - Predictive Maintenance Tutorial - Linear MTLR - Number of machines experiencing a failure" title="PySurvival - Predictive Maintenance Tutorial - Linear MTLR - Number of machines experiencing a failure" width=100%, height=100%  /></center>
<center>Figure 10 - Actual vs Predicted - Number of machines experiencing a failure</center></p>
<p>Based on the performance metrics, it was expected that the time series would be very close; here the model makes an average error of ~1 machine throughout the entire timeline.</p>
<h3 id="72-individual-predictions">7.2 - Individual predictions</h3>
<p>Now that we know that we can provide reliable predictions for an entire cohort. Let's compute the survival predictions at the individual level.</p>
<p>First, we can construct the risk groups based on risk scores distribution. The helper function <code>create_risk_groups</code>, which can be found in <code>pysurvival.utils</code>, will help us do that:
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.display</span> <span style="color: #008000; font-weight: bold">import</span> create_risk_groups

risk_groups <span style="color: #666666">=</span> create_risk_groups(model<span style="color: #666666">=</span>linear_mtlr, X<span style="color: #666666">=</span>X_test,
    use_log <span style="color: #666666">=</span> <span style="color: #008000">True</span>, num_bins<span style="color: #666666">=50</span>, figure_size<span style="color: #666666">=</span>(<span style="color: #666666">20</span>, <span style="color: #666666">4</span>),
    low<span style="color: #666666">=</span> {<span style="color: #BA2121">&#39;lower_bound&#39;</span>:<span style="color: #666666">0</span>, <span style="color: #BA2121">&#39;upper_bound&#39;</span>:<span style="color: #666666">1.8</span>, <span style="color: #BA2121">&#39;color&#39;</span>:<span style="color: #BA2121">&#39;red&#39;</span>},
    medium<span style="color: #666666">=</span> {<span style="color: #BA2121">&#39;lower_bound&#39;</span>: <span style="color: #666666">1.8</span>, <span style="color: #BA2121">&#39;upper_bound&#39;</span>:<span style="color: #666666">1.93</span>, <span style="color: #BA2121">&#39;color&#39;</span>:<span style="color: #BA2121">&#39;green&#39;</span>},
    high<span style="color: #666666">=</span> {<span style="color: #BA2121">&#39;lower_bound&#39;</span>:<span style="color: #666666">1.93</span>, <span style="color: #BA2121">&#39;upper_bound&#39;</span>:<span style="color: #666666">2.1</span>, <span style="color: #BA2121">&#39;color&#39;</span>:<span style="color: #BA2121">&#39;blue&#39;</span>}
    )
</pre></div></p>
<p><center><img src="images/maintenance_risk.png" alt="PySurvival - Predictive Maintenance Tutorial - Linear MTLR - Risk groups" title="PySurvival - Predictive Maintenance Tutorial - Linear MTLR - Risk groups" width=100%, height=100%  /></center>
<center>Figure 11 - Creating Risk groups</center></p>
<p><em>Note: The current choice of the lower and upper bounds for each group is based on my intuition; so feel free to change the values so as to match your situation instead.</em></p>
<p>Here, we can see that 3 main groups, <em>low</em>, <em>medium</em> and <em>high</em> risk groups, can be created. Because the C-index is high, the model will be able to perfectly rank the survival times of a random unit of each group, such that <script type="math/tex"> t_{high}  \leq t_{medium} \leq t_{low}</script>.</p>
<p>Let's randomly select individual unit in each group and compare the survival functions. To demonstrate our point, we will purposely select units which experienced an event to visualize the actual time of event.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Initializing the figure</span>
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(figsize<span style="color: #666666">=</span>(<span style="color: #666666">15</span>, <span style="color: #666666">8</span>))

<span style="color: #408080; font-style: italic"># Selecting a random individual that experienced failure from each group</span>
groups <span style="color: #666666">=</span> []
<span style="color: #008000; font-weight: bold">for</span> i, (label, (color, indexes)) <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>(risk_groups<span style="color: #666666">.</span>items()) :

    <span style="color: #408080; font-style: italic"># Selecting the individuals that belong to this group</span>
    <span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(indexes) <span style="color: #666666">==</span> <span style="color: #666666">0</span> :
        <span style="color: #008000; font-weight: bold">continue</span>
    X <span style="color: #666666">=</span> X_test<span style="color: #666666">.</span>values[indexes, :]
    T <span style="color: #666666">=</span> T_test<span style="color: #666666">.</span>values[indexes]
    E <span style="color: #666666">=</span> E_test<span style="color: #666666">.</span>values[indexes]

    <span style="color: #408080; font-style: italic"># Randomly extracting a machine that experienced failure from each group</span>
    choices <span style="color: #666666">=</span> np<span style="color: #666666">.</span>argwhere((E<span style="color: #666666">==1.</span>))<span style="color: #666666">.</span>flatten()
    <span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(choices) <span style="color: #666666">==</span> <span style="color: #666666">0</span> :
        <span style="color: #008000; font-weight: bold">continue</span>
    k <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>choice( choices, <span style="color: #666666">1</span>)[<span style="color: #666666">0</span>]

    <span style="color: #408080; font-style: italic"># Saving the time of event</span>
    t <span style="color: #666666">=</span> T[k]

    <span style="color: #408080; font-style: italic"># Computing the Survival function for all times t</span>
    survival <span style="color: #666666">=</span> linear_mtlr<span style="color: #666666">.</span>predict_survival(X[k, :])<span style="color: #666666">.</span>flatten()

    <span style="color: #408080; font-style: italic"># Displaying the functions</span>
    label_ <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;{} risk&#39;</span><span style="color: #666666">.</span>format(label)
    plt<span style="color: #666666">.</span>plot(linear_mtlr<span style="color: #666666">.</span>times, survival, color <span style="color: #666666">=</span> color, label<span style="color: #666666">=</span>label_, lw<span style="color: #666666">=2</span>)
    groups<span style="color: #666666">.</span>append(label)

    <span style="color: #408080; font-style: italic"># Actual time</span>
    plt<span style="color: #666666">.</span>axvline(x<span style="color: #666666">=</span>t, color<span style="color: #666666">=</span>color, ls <span style="color: #666666">=</span><span style="color: #BA2121">&#39;--&#39;</span>)
    ax<span style="color: #666666">.</span>annotate(<span style="color: #BA2121">&#39;T={:.1f}&#39;</span><span style="color: #666666">.</span>format(t), xy<span style="color: #666666">=</span>(t, <span style="color: #666666">0.5*</span>(<span style="color: #666666">1.+0.2*</span>i)),
        xytext<span style="color: #666666">=</span>(t, <span style="color: #666666">0.5*</span>(<span style="color: #666666">1.+0.2*</span>i)), fontsize<span style="color: #666666">=12</span>)

<span style="color: #408080; font-style: italic"># Show everything</span>
groups_str <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;, &#39;</span><span style="color: #666666">.</span>join(groups)
title <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;Comparing Survival functions between {} risk grades&quot;</span><span style="color: #666666">.</span>format(groups_str)
plt<span style="color: #666666">.</span>legend(fontsize<span style="color: #666666">=12</span>)
plt<span style="color: #666666">.</span>title(title, fontsize<span style="color: #666666">=15</span>)
plt<span style="color: #666666">.</span>ylim(<span style="color: #666666">0</span>, <span style="color: #666666">1.05</span>)
plt<span style="color: #666666">.</span>show()
</pre></div></p>
<p><center><img src="images/maintenance_individual.png" alt="PySurvival - Predictive Maintenance Tutorial - Linear MTLR - Predicting individual survival functions" title="PySurvival - Predictive Maintenance Tutorial - Linear MTLR - Predicting individual survival functions" width=100%, height=100%  /></center>
<center>Figure 12 - Predicting individual survival functions</center></p>
<p>As we can see, the model manages to perfectly predict the event time, here it corresponds to a sudden drop in the individual survival function.</p>
<hr />
<h2 id="8-conclusion">8 - Conclusion</h2>
<p>We can now save our model so as to put it in production and score future machines.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Let&#39;s now save our model</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils</span> <span style="color: #008000; font-weight: bold">import</span> save_model
save_model(linear_mtlr, <span style="color: #BA2121">&#39;/Users/xxx/Desktop/pdm_linear_mtlr.zip&#39;</span>)
</pre></div></p>
<p>In this example, we have shown that it is possible to predict with great degree of certainty when a machine will fail. The Data Science team could predict the machines survival function every day, so that 1 or 2 weeks before the machine is supposed to fail, the factory manager is notified so that the necessary actions can be taken.</p>
<hr />
<h2 id="references">References</h2>
<ul>
<li><a href="https://en.wikipedia.org/wiki/Predictive_maintenance">https://en.wikipedia.org/wiki/Predictive_maintenance</a></li>
<li><a href="http://www.essec.edu/en/">Essec Business School - Course in Business Analytics</a></li>
<li><a href="https://github.com/ludovicbenistant/Management-Analytics/blob/master/Supply%20Chain/Maintenance%20(survival%20analysis)/maintenance_data.csv">Maintenance dataset</a></li>
<li><a href="https://github.com/nicolasfguillaume/Strategic-Business-Analytics-with-R/blob/master/module4.md">https://github.com/nicolasfguillaume/Strategic-Business-Analytics-with-R/blob/master/module4.md</a></li>
</ul>









            </article>
          </div>
        </div>
      </main>


<footer class="md-footer">

  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-footer-copyright">

          <div class="md-footer-copyright__highlight">
            Copyright &copy; 2019 Square Inc.
          </div>



        brought to you by
        <a href="https://squareup.com/us">
        <img src="images/Square_logo.png" alt="Square Logo" title="Square Logo" width=20%, height=20% align="center"  /></a>


      </div>

  <div class="md-footer-social">
    <link rel="stylesheet" href="../assets/fonts/font-awesome.css">

      <a href="https://pysurvival.io/" class="md-footer-social__link fa fa-home"></a>

      <a href="https://github.com/square/pysurvival" class="md-footer-social__link fa fa-github"></a>

  </div>

    </div>
  </div>
</footer>

    </div>

      <script src="../assets/javascripts/application.d9aa80ab.js"></script>

      <script>app.initialize({version:"1.0.4",url:{base:".."}})</script>

        <script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML"></script>

        <script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/0.5.7/chartjs-plugin-annotation.js"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


  </body>
</html>
